const { get, addAddress, deleteAddress, amendAddress, update, findOne, delImgs, search } = require("../api/address.js")

var express = require('express');

// 创建了一个路由器
var router = express.Router();


//获取地址列表
router.get('/', async function (req, res, next) {
    //获取参数从请求头
    const { curPage, eachPage } = req.query;
    let data = await get({ curPage, eachPage });
    res.send(data);
})

//新增自提点
router.post('/add-address', async function (req, res, next) {
    const { name, Address, longitude, latitude, Cold_store, Phone, Open_hours } = req.body;
    //data 获取新增成功返回新增完整的那条数据
    const data = await addAddress({ name, Address, longitude, latitude, Cold_store, Phone, Open_hours })
    res.send(data)
})

// 删除自提点
router.delete('/delet-address', async function (req, res, next) {
    const { _id } = req.body;
    const data = await deleteAddress(_id);
    res.send(data);
});

//修改自提点

router.put('/amend-address', async function (req, res, next) {
    const newAddress = req.body;
    await amendAddress(newAddress)
    //返回状态值
    res.send({ satte: true })
})

//上传图片
// 更新图片数据
router.patch('/:_id', async function (req, res, next) {
    // 从url拿参数
    const { _id } = req.params;
    const data = req.body;
    // 传值更新
    const result = await update({ _id }, data);
    // // 返回更新的整条数据
    // const result = await findOne(_id);
    res.send(result)
});


// 删除照片路由
router.delete('/delImag', async function (req, res, next) {
    // 从url拿参数
    const { _id, Image } = req.body;
    await delImgs({ _id, Image });
    //获取到修改保存后的最新数据并返回
    const data = await findOne({ _id });
    res.send(data);

});
//搜索
router.get('/search', async function (req, res, next) {
    const { value } = req.query
    const SerchDate = await search({ value })

    res.send(SerchDate)

})

module.exports = router;